<template>
	<z-paging ref="pagingRef" v-model="dataList" @query="queryList">
		<view class="income-summary-layout">
			<!-- 筛选 -->
			<view class="income-summary-filter">
				<view class="income-summary-filter-left" @tap="showSelectDate">
					<view class="income-summary-filter-left-date">
						<text>{{dayjs(selectDateValue).format('YYYY年MM月')}}</text>
						<xh-icon icon="icon_shanglajiantou"></xh-icon>
					</view>
					<view class="income-summary-filter-left-money">
						<text>月统计：</text>
						<text>¥{{totalMoney}}</text>
					</view>
				</view>
				<view class="income-summary-filter-right" @tap="showFilter">
					<image src="/static/wallet/wallet_sy_filter_icon.png" mode="aspectFit"></image>
					<text>筛选</text>
				</view>
			</view>
			<!-- 列表 -->
			<view class="income-summary-list">
				<view class="income-summary-list-cell" v-for="item in dataList" :key="item.id" @tap="showDetail(item)">
					<image src="/static/wallet/wallet_sy_list_icon.png" mode="aspectFit"></image>
					<view class="income-summary-list-cell-container">
						<text>{{getTypeName(item)}}</text>
						<text>{{dayjs(item.tradeDate).format('YYYY-MM-DD')}}</text>
						<text>{{item.productPolicy}}</text>
					</view>
					<view class="income-summary-list-cell-right">
						<text>¥{{item.amount}}</text>
						<xh-icon icon="icon_jiantou"></xh-icon>
					</view>
					<view class="line"></view>
				</view>
			</view>
			<uv-datetime-picker ref="datePickerRef" v-model="selectDateValue" mode="year-month" @confirm="confirm"></uv-datetime-picker>
			<xh-income-summary-filter-pop ref="filterRef" @filterResult="filterResult"></xh-income-summary-filter-pop>
		</view>

	</z-paging>

</template>

<script setup>
	import { onLoad } from '@dcloudio/uni-app'
	import { ref } from 'vue'
	import dayjs from 'dayjs'
	import XhIncomeSummaryFilterPop from './widget/xh-income-summary-filter-pop.vue'
	import { getIncomeStatisticsData,getIncomeStatisticsListData } from '@/common/api/api.js'
	import { useConfigStore } from '@/common/stores'
	const configStore = useConfigStore()
	const datePickerRef = ref(null)
	const filterRef = ref(null)
	const selectDateValue = ref(Number(new Date()))
	const dataList = ref([])
	const pagingRef = ref(null)
	// 累计金额
	const totalMoney = ref(0)
	const queryParams = ref({
		typeId:'',
		policyId:'',
		startDate:'',
		endDate:''
	})
	// 选择日期
	const showSelectDate = () => {
		datePickerRef.value.open()
	}
	// 日期选择回调
	const confirm = (res) => {
		selectDateValue.value = res.value
		queryParams.value.startDate = dayjs(selectDateValue.value).startOf('month').format('YYYY-MM-DD')
		queryParams.value.endDate = dayjs(selectDateValue.value).endOf('month').format('YYYY-MM-DD')
		pagingRef.value.reload()
	}
	// 筛选
	const showFilter = () => {
		filterRef.value.open(queryParams.value.typeId,queryParams.value.policyId)
	}
	// 筛选结果回调
	const filterResult = (res) => {
		queryParams.value.typeId = res.typeId
		queryParams.value.policyId = res.policyId
		pagingRef.value.reload()
	}
	// 查看汇总明细
	const showDetail = (item) => {
		console.log(item)
		const dataStr = decodeURIComponent(JSON.stringify(item))
		uni.navigateTo({
			url: `/pages/wallet/income-summary/income-summary-detail/income-summary-detail?data=${dataStr}`
		})
	}
	// 根据类型code获取类型名称
	const getTypeName = (item)=>{
		const res = configStore.incomeTypes.filter(obj=>item.revenueType == obj.code)
		if(res.length > 0){
			return res[0].name
		}
		return ''
	}
	// 获取列表数据
	const queryList = async (pageNo) => {
		console.log(queryParams.value)
		let params = {}
		params.startDate = queryParams.value.startDate
		params.endDate = queryParams.value.endDate
		params.dateType = '1'
		if(!uni.$uv.test.empty(queryParams.value.typeId)){
			params.incomeType = queryParams.value.typeId
		}
		if(!uni.$uv.test.empty(queryParams.value.policyId)){
			params.policyId = queryParams.value.policyId
		}
		getIncomeStatisticsMoney(params)
		const res = await getIncomeStatisticsListData(params, pageNo)
		const list = res.result
		dataList.value = [...dataList.value,...list]
		pagingRef.value.completeByTotal(list,res.page.totalElements)
	}
	// 获取统计金额
	const getIncomeStatisticsMoney = async (params)=>{
		const res = await getIncomeStatisticsData(params)
		totalMoney.value = res.result.amount
	}
	onLoad(async ()=>{
		queryParams.value.startDate = dayjs(selectDateValue.value).startOf('month').format('YYYY-MM-DD')
		queryParams.value.endDate = dayjs(selectDateValue.value).endOf('month').format('YYYY-MM-DD')
		await configStore.updateIncomeTypes()
	})
</script>

<style lang="scss" scoped>
	.income-summary-layout {
		background-color: #fff;
		.income-summary-filter {
			display: flex;
			align-items: center;
			justify-content: space-between;
			background-color: $xh-bg-color;
			padding: 30rpx;

			.income-summary-filter-left {
				.income-summary-filter-left-date {
					display: flex;
					align-items: center;

				}

				.income-summary-filter-left-money {
					display: flex;
					align-items: center;
					color: $xh-color-9;
					font-size: 24rpx;
					margin-top: 10rpx;

					text:nth-child(2) {
						color: $xh-color-3;
					}
				}
			}

			.income-summary-filter-right {
				display: flex;
				align-items: center;
				background-color: #fff;
				padding: 0 30rpx;
				border: 1px solid #eee;
				height: 60rpx;
				border-radius: 30rpx;

				image {
					width: 24rpx;
					height: 24rpx;
					margin-right: 10rpx;


				}
			}
		}

		.income-summary-list-cell {
			display: flex;
			align-items: center;
			padding: 30rpx;
			position: relative;

			image {
				width: 60rpx;
				height: 60rpx;
				flex-shrink: 0;
			}

			.income-summary-list-cell-container {
				display: flex;
				flex-direction: column;
				margin-left: 20rpx;
				font-size: 24rpx;
				color: $xh-color-9;

				text:nth-child(1) {
					font-size: 28rpx;
					color: $xh-color-3;
				}

				text:nth-child(2) {
					margin: 16rpx 0;
				}
			}

			.income-summary-list-cell-right {
				margin-left: auto;
			}
		}
	}
</style>